home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / prog / strpp212.zip / STR.H < prev    next >
C/C++ Source or Header  |  1993-02-17  |  10KB  |  257 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 2.12                                       02/16/93 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1993 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* str.h                                                                */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _STR_H
  11. #define _STR_H
  12.  
  13. #include <string.h>
  14. #if defined (__ZTC__)
  15. #include <stream.hpp>            // Zortech
  16. #else
  17. #include <iostream.h>            // Borland
  18. #endif
  19.  
  20. #define LEFT_JUSTIFY    0        // obsolete - use LEFT
  21. #define CENTER_JUSTIFY    1        // obsolete - use CENTER
  22. #define RIGHT_JUSTIFY    2        // obsolete - use RIGHT
  23. #define LEFT        0
  24. #define CENTER          1
  25. #define RIGHT            2
  26. #define NOCLIP        0
  27. #define CLIP        1
  28. #define WHITESPACE    0
  29.  
  30. class string
  31. {
  32. private:
  33.   char *_ptr;
  34.   void ltos(long n);
  35.  
  36. public:
  37.   string();                // default constructor;
  38.   string(const char c,            // initialize with a character,
  39.          unsigned n = 1);        //   optional # of characters
  40.   string(const char *s,            // initialize with a char *,
  41.          unsigned pos = 0,        //   optional starting char
  42.          unsigned len = 10000);        //   optional # of chars
  43.   string(const string& s,        // initialize with another string,
  44.          unsigned pos = 0,        //   optional starting char
  45.          unsigned len = 10000);        //   optional # of chars
  46.   string(int n)  { ltos((long)n); }    // initialize with an integer
  47.   string(long n) { ltos(n); }        // initialize with a long int
  48.  
  49.  ~string(void);
  50.  
  51.   operator char*() const                     { return _ptr; }
  52.   char* ptr(void) const                      { return _ptr; }
  53.   const char* operator()() const             { return _ptr; }
  54.   const char* operator()(unsigned pos) const { return _ptr + pos; }
  55.   string      operator()(unsigned pos, unsigned len) const;
  56.  
  57.   int     Length(void) const { return strlen(_ptr); }
  58.   int     Len(void)    const { return strlen(_ptr); }
  59.   string& toUpper(void);        // convert str to uppercase
  60.   string& toLower(void);        // convert str to lowercase
  61.   int&    Value(int& n);        // convert str to an integer
  62.   long&   Value(long& n);        // convert str to a long int
  63.  
  64.   string& Left(unsigned len);        // left   len chars
  65.   string& Right(unsigned len);        // right  len chars
  66.   string& Mid(unsigned pos,        // middle len chars from pos
  67.               unsigned len);
  68.   string& Justify(int mode,        // justify str according to mode
  69.                   unsigned len,
  70.           int clip=0);
  71.   string& Trim(int mode = CENTER,    // Delete leading/trailing whitespace
  72.                char ch = WHITESPACE);
  73.  
  74.   string& Insert(unsigned pos,        // insert substring
  75.                  const string& s);
  76.   string& Delete(unsigned pos,        // delete substring
  77.                  unsigned len = 10000);
  78.   char*   Copy(char*&);            // copy str to char* (non-const)
  79.  
  80.   int     Index(const string& t);    // position of t in str
  81.   string  SubStr(unsigned p,        // substring of str at position p
  82.                  unsigned n=10000);
  83.   int     Split(string*& a,        // split str into an array a on
  84.                 const string& fs);    //   field separator fs
  85.   int     Sub(const string& from,    // substitute from with to in str
  86.               const string& to,
  87.           unsigned count=10000);
  88.  
  89.   string& operator=(const char);    // str1 = char
  90.   string& operator=(const char*);    // str1 = char*
  91.   string& operator=(const string&);    // str1 = str
  92.   string& operator=(const int);        // str1 = n
  93.   string& operator=(const long);    // str1 = n
  94.   string& operator=(const float);    // str1 = x
  95.   string& operator=(const double);    // str1 = x
  96.   string& operator+=(const char);    // str1 += char
  97.   string& operator+=(const char*);    // str1 += char*
  98.   string& operator+=(const string&);    // str1 += str
  99.   string& operator*=(unsigned n);    // str1 *= n
  100.   char&   operator[](unsigned i) const;    // ch = str[i] or str[i] = ch
  101.  
  102.   friend string operator +  (const string&, const string&);
  103.   friend string operator +  (const string&, const char*);
  104.   friend string operator +  (const char*,   const string&);
  105.   friend string operator *  (const string&, int n);
  106.   friend int    operator == (const string&, const string&);
  107.   friend int    operator == (const string&, const char*);
  108.   friend int    operator == (const char*,   const string&);
  109.   friend int    operator != (const string&, const string&);
  110.   friend int    operator != (const string&, const char*);
  111.   friend int    operator != (const char*,   const string&);
  112.   friend int    operator <  (const string&, const string&);
  113.   friend int    operator <  (const string&, const char*);
  114.   friend int    operator <  (const char*,   const string&);
  115.   friend int    operator >  (const string&, const string&);
  116.   friend int    operator >  (const string&, const char*);
  117.   friend int    operator >  (const char*,   const string&);
  118.   friend int    operator <= (const string&, const string&);
  119.   friend int    operator <= (const string&, const char*);
  120.   friend int    operator <= (const char*,   const string&);
  121.   friend int    operator >= (const string&, const string&);
  122.   friend int    operator >= (const string&, const char*);
  123.   friend int    operator >= (const char*,   const string&);
  124.  
  125.   /* --- Awk-style functions ------------------------------------------ */
  126.  
  127.   friend int    length(const string& s) { return s.Len(); }
  128.   friend int    index(const string& s, const string& t);
  129.   friend string substr(const string& s, unsigned p, unsigned n=10000);
  130.   friend int    split(const string& s, string*& a, const string& fs);
  131.   friend int    gsub(const string& from, const string& to, string& str, unsigned count=10000);
  132.   friend int    sub(const string& from, const string& to, string& str);
  133.   friend int    match(const string& s, const string& r);
  134.  
  135.   /* --- Other C-style functions -------------------------------------- */
  136.  
  137.   friend string toupper(const string& s);
  138.   friend string tolower(const string& s);
  139.   friend string left(const string& s, unsigned n);
  140.   friend string right(const string& s, unsigned n);
  141.   friend string mid(const string& s, unsigned p, unsigned n);
  142.   friend string justify(const string& s, int mode, unsigned len, int clip=1);
  143.   friend string trim(const string& s, int mode=CENTER);
  144.  
  145.   /* --- Obsolete naming conventions ---------------------------------- */
  146.  
  147.   int     length(void) const { return strlen(_ptr); }
  148.   int     len(void)    const { return strlen(_ptr); }
  149.  
  150.   string  left(unsigned len) const { string tmp; return tmp.Left(len); }
  151.   string  right(unsigned len) const { string tmp; return tmp.Right(len); }
  152.   string  mid(unsigned pos, unsigned len) const { string tmp; return tmp.Mid(pos, len); }
  153.   string  justify(int mode, unsigned len, int clip=0) const { string tmp; return tmp.Justify(mode, len, clip); }
  154.  
  155.   string& toupper(void) { return toUpper(); }
  156.   string& tolower(void) { return toLower(); }
  157. };
  158.  
  159. typedef string String;
  160.  
  161. ostream& operator<<(ostream&, const string&);
  162. istream& operator>>(istream&, string&);
  163.  
  164. inline int operator==(const string& s1, const char* s2) {
  165.   return strcmp(s1._ptr,s2)==0;
  166. }
  167.  
  168. inline int operator==(const char* s1, const string& s2) {
  169.   return strcmp(s1,s2._ptr)==0;
  170. }
  171.  
  172. inline int operator==(const string& s1, const string& s2) {
  173.   return strcmp(s1._ptr,s2._ptr)==0;
  174. }
  175.  
  176. inline int operator!=(const string& s1, const char *s2) {
  177.   return strcmp(s1._ptr,s2)!=0;
  178. }
  179.  
  180. inline int operator!=(const char *s1, const string& s2) {
  181.   return strcmp(s1,s2._ptr)!=0;
  182. }
  183.  
  184. inline int operator!=(const string& s1, const string& s2) {
  185.   return strcmp(s1._ptr,s2._ptr)!=0;
  186. }
  187.  
  188. inline int operator<(const string& s1, const char *s2) {
  189.   return strcmp(s1._ptr,s2)< 0;
  190. }
  191.  
  192. inline int operator<(const char *s1, const string& s2) {
  193.   return strcmp(s1,s2._ptr)< 0;
  194. }
  195.  
  196. inline int operator<(const string& s1, const string& s2) {
  197.   return strcmp(s1._ptr,s2._ptr)< 0;
  198. }
  199.  
  200. inline int operator>(const string& s1, const char *s2) {
  201.   return strcmp(s1._ptr,s2)> 0;
  202. }
  203.  
  204. inline int operator>(const char *s1, const string& s2) {
  205.   return strcmp(s1,s2._ptr)> 0;
  206. }
  207.  
  208. inline int operator>(const string& s1, const string& s2) {
  209.   return strcmp(s1._ptr,s2._ptr)> 0;
  210. }
  211.  
  212. inline int operator<=(const string& s1, const char *s2) {
  213.   return strcmp(s1._ptr,s2)<=0;
  214. }
  215.  
  216. inline int operator<=(const char *s1, const string& s2) {
  217.   return strcmp(s1,s2._ptr)<=0;
  218. }
  219.  
  220. inline int operator<=(const string& s1, const string& s2) {
  221.   return strcmp(s1._ptr,s2._ptr)<=0;
  222. }
  223.  
  224. inline int operator>=(const string& s1, const char *s2) {
  225.   return strcmp(s1._ptr,s2)>=0;
  226. }
  227.  
  228. inline int operator>=(const char *s1, const string& s2) {
  229.   return strcmp(s1,s2._ptr)>=0;
  230. }
  231.  
  232. inline int operator>=(const string& s1, const string& s2) {
  233.   return strcmp(s1._ptr,s2._ptr)>=0;
  234. }
  235.  
  236. inline int string::Index(const string& t) {
  237.   return index(*this, t);
  238. }
  239.  
  240. inline string string::SubStr(unsigned p, unsigned n) {
  241.   return substr(*this, p, n);
  242. }
  243.  
  244. inline int string::Split(string*& a, const string& fs) {
  245.   return split(*this, a, fs);
  246. }
  247.  
  248. inline int string::Sub(const string& from, const string& to, unsigned count) {
  249.   return gsub(from, to, *this, count);
  250. }
  251.  
  252. inline int sub(const string& from, const string& to, string& str) {
  253.   return gsub(from, to, str, 1);
  254. }
  255.  
  256. #endif
  257.